package distrcomp;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.rmi.ssl.SslRMIClientSocketFactory;

public class Administrator {
	public static void main(String[] args) throws RemoteException, AlreadyBoundException {
		String regStr;
		if(args.length > 1) {
			regStr = args[1]; 
		} else {
			regStr = "localhost";
		}
		
		Registry registry = LocateRegistry.getRegistry(regStr, 1099, new SslRMIClientSocketFactory());
		try {
			Admin admin = (Admin) registry.lookup("AdminService");
			
			//List pending shipments
			
			System.out.println("Pending shipments:");
			System.out.println("------------------");
			System.out.println();
			ShipmentIterator pending = admin.getPendingShipments();
			try {
				Shipment shipment = pending.next();
				while(shipment != null) {
					shipment.print();
					shipment = pending.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//List history shipments
			
			System.out.println("History of shipments:");
			System.out.println("---------------------");
			System.out.println();
			ShipmentIterator history = admin.getShipmentHistory();
			try {
				Shipment shipment = history.next();
				while(shipment != null) {
					shipment.print();
					shipment = history.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//Backup slave with ID 0
			admin.backup(0);
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}
